/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tracker.stub;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import tracker.entity.PointsEntity;
import tracker.entity.PointsEntityFacade;

/**
 * Workshop Class ID : SE0691 Student ID : SE90009 Student Name : Phạm Nguyễn
 * Nhật Trung Due Date : 12 November 2012 I declare that this assignment is my
 * own work in accordance with FPT Policy.
 */
public class PointsSessionStub {

    PointsEntityFacade pointsEntityFacade = lookupPointsEntityFacadeBean();

    public void insert(PointsEntity entity) {
        pointsEntityFacade.create(entity);
    }

    public PointsEntity findUserById(int userId) {
        return pointsEntityFacade.find(userId);
    }

    public void deleteById(int userId) {
        PointsEntity entity = findUserById(userId);
        pointsEntityFacade.remove(entity);
    }

    public void change(PointsEntity entity) {
        pointsEntityFacade.edit(entity);
    }

    public List<PointsEntity> findAll() {
        return pointsEntityFacade.findAll();
    }

    public List<PointsEntity> findAllTorrentsByUserId(int userId) {
        return pointsEntityFacade.findAllTorrentsByUserId(userId);
    }

    public List<PointsEntity> findAllTorrentsOfUserIdByTorrentType(int userId, int torrentType) {
        return pointsEntityFacade.findAllTorrentsOfUserIdByTorrentType(userId, torrentType);
    }

    public Float getPointsDependTorrentsByUserId(int userId){
        return pointsEntityFacade.getPointsDependTorrentsByUserId(userId);
    }
    
    public List<PointsEntity> getAllUsersByTorrentId(int torrentId){
        return pointsEntityFacade.findAllUsersByTorrentId(torrentId);
    }
    
    public List<PointsEntity> findAllUserByTorrentIdOfTypeId(int torrentId, int torrentType){
        return pointsEntityFacade.findAllUserByTorrentIdOfTypeId(torrentId, torrentType);
    }
    
    private PointsEntityFacade lookupPointsEntityFacadeBean() {
        try {
            Context c = new InitialContext();
            return (PointsEntityFacade) c.lookup("java:global/ASG2-Tracker1/ASG2-Tracker1-ejb/PointsEntityFacade!tracker.entity.PointsEntityFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
